﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace HostDetector
{
    //0 - PlayerID
    //12-17 - local IP #1
    //18-23 - local IP #2
    //24-29 - local IP #3
    //30-35 - Public IP
    //44-(44+name length) Player name
    //46+name length - Player Level
    public class Player
    {

        public int PlayerID
        {
            get { return playerID; }
        }
        public string Name
        {
            get { return name; }
        }
        public int Level
        {
            get { return level; }
        }

        public IPEndPoint Ip
        {
            get { return ip; }
        }
        
        string name;

        IPEndPoint ip;

        
        int playerID;


        int level;

        

        Encoding encoder = Encoding.GetEncoding("iso-8859-1");
        public Player(byte[] data)
        {
            int nameStart =  44;
            int nameLength = 0;
            while (data[nameStart + nameLength] != 0)
                        nameLength++;
            name = encoder.GetString(data, nameStart, nameLength);
            var ipBytes = new byte[4];
            int ipPos = 30;
            if (data[30] == 0)//this is a faulty assumption, if someone has 3 local IPs, it will fail
                ipPos++;
            Array.Copy(data,ipPos,ipBytes,0,4);
            level = data[46 + nameLength] +1;
            ip = new IPEndPoint(new IPAddress(ipBytes), BitConverter.ToUInt16(data, ipPos + 4));
            playerID = data[0];
            Console.WriteLine(data[0]+", "+ level+", " +name + ": " + ip);
        }
    }
}
